window.onload = function () {
    document.getElementById("post-setting").className = "add_class";
    document.getElementById("system-management").className = "add-nav-active";
    document.getElementById("organizational_management_ul").style.display = "inline-block";
    document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
    station_list();
};

function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function click_ul2() {
    if (document.getElementById("jiantou2").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-xia";
        document.getElementById("SuperSU_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-shang";
        document.getElementById("SuperSU_ul").style.display = "none";
    }
}

function pagenum(dataTotals=0, page=1) {
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            station_list(sent_str='', page);
            document.getElementById("check_all").checked = false;
        }
    });
}

//
function delete_val() {
    // 创建左侧架构目录
    document.getElementById("Ztree-label").innerHTML = "";
    document.getElementById("checked-label").innerHTML = "";
    document.getElementById("checkboxID").checked = false;
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/organization.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.JSONObject = JsonDate["data"][0]["check_dict"];
                create_left_catalog(JsonDate["data"][0]["tree_data"], "model");
            }
        }
    };
}

// 创建左侧架构目录
function create_left_catalog(zNodes, create_location="none") {
    if (create_location === "model") {
        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: treenodeClick
            }
        };
        $.fn.zTree.init($("#treeDemo1"), setting, zNodes);
    }
}

// 点击获取当前节点及所有子节点
function treenodeClick(event, treeId, treeNode, clickFlag) {
    //此处treeNode 为当前节点
    var str ='' ;
    str = getAllChildrenNodes(treeNode,str);
    // 去掉最前面的逗号
    str = str.substring(1, str.length);
    // 加上被选择节点自己
    var count = treeNode.name + ',' + str;
    // 得到所有节点ID 的数组
    var countArray = str.split(',');

    // 点击之后的业务流程
    var Ztree_label = document.getElementById("Ztree-label");
    Ztree_label.innerHTML = "";
    document.getElementById("checkboxID").checked = false;
    var ul = document.createElement("ul");
    var li = document.createElement("li");
    ul.setAttribute("id", "label-ul");
    li.className = "model_li";
    li.style.display = "inline-block";
    li.setAttribute("onclick", "click_val(this)");
    li.setAttribute("value", treeNode.name);
    li.innerHTML = treeNode.name;
    ul.appendChild(li);

    for (var i=0; i<countArray.length; i++) {
        var li1 = document.createElement("li");
        li1.className = "model_li";
        li1.setAttribute("onclick", "click_val(this)");
        li1.setAttribute("value", countArray[i]);
        li1.innerHTML = countArray[i];
        ul.appendChild(li1);
    }
    Ztree_label.appendChild(ul);
    document.getElementById("checked-label").innerHTML = "";

}

// 递归，获取所有子节点
function getAllChildrenNodes(treeNode,result){
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
                result += ',' + childrenNodes[i].name;
                result = getAllChildrenNodes(childrenNodes[i], result);
            }
        }
    }
    return result;
}

// 点击标签选中
function click_val(event) {
    var model_li_list = document.getElementsByClassName("model_li");
    for (var i=0; i<model_li_list.length; i++) {
        model_li_list[i].style.color = "#3f414d";
    }
    event.style.color = "#4169E1";
    var checked_label = document.getElementById("checked-label");
    checked_label.innerHTML = "";
    var p_tag = document.createElement("p");
    p_tag.setAttribute("id", "checked_ptag");
    p_tag.style.cursor = "pointer";
    p_tag.innerHTML = event.innerText;
    p_tag.style.color = "#3f414d";
    checked_label.appendChild(p_tag);
}

// 检查复选框的选择状态
function check_input() {
    var model_li_list = document.getElementsByClassName("model_li");
    if(document.getElementById("checkboxID").checked){
        for (var i=0; i<model_li_list.length; i++) {
            model_li_list[i].style.display = "inline-block";
        }
    }else {
        for (var t=1; t<model_li_list.length; t++) {
            model_li_list[t].style.display = "none";
        }
    }
}

// 点击模态框确认按钮选择上级部门
function summit_val() {
    if (document.getElementById("checked_ptag")){
        document.getElementById("parent").value = document.getElementById("checked_ptag").innerText;
        document.getElementById("model-close1").click();
        document.getElementById("parent").removeAttribute("style")
    }else {
        alert("请选择部门！")
    }
}

// 检测输入框数据
function check_val(ele_id, ele_placeholder) {
    if (document.getElementById(ele_id).value === ""){
        document.getElementById(ele_id).style.borderColor = "red";
    }else {
        document.getElementById(ele_id).removeAttribute("style")
    }
}

// 提交新的岗位并入库
function submit_data() {
    if (document.getElementById("parent").value === ""){
        document.getElementById("parent").style.borderColor = "red";
        return false
    }else {
        document.getElementById("parent").removeAttribute("style")
    }
    var parent = document.getElementById("parent").value;       // 所属部门
    var stationNum = document.getElementById("stationNum").value;       // 岗位编号
    var stationName = document.getElementById("stationName").value;       // 岗位名称

    var obj = document.getElementById("category"); //定位id
    var index = obj.selectedIndex; // 选中索引
    var category = obj.options[index].text;                     // 岗位类别
    var position_statement = document.getElementById("position_statement").value;           // 岗位职责
    var job_description = document.getElementById("job_description").value;           // 岗位描述
    var sent_str = "parent="+String(parent)+"&stationNum="+String(stationNum)+"&stationName="+String(stationName)+"&category="+String(category)+"&position_statement="+String(position_statement)+"&job_description="+String(job_description);
    station_list(sent_str);
    document.getElementById("model-close").click();
}

// 加载岗位列表
function station_list(sent_str="", page=1) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/set_up.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send(sent_str+"&page="+String(page));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], page);
            }
        }
    };
}

function create_table(count_num, json_data) {
    document.getElementById("close-model2").click();
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
        td.innerHTML = "<input id='"+json_data[i]["stationNum"]+"' class='check_box' type=\"checkbox\">";td.style.background="#f8f9fa";td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["parent"];td1.title = json_data[i]["parent"];td1.style.width = "8%";td1.style.textAlign = "center";
        td2.innerHTML = json_data[i]["stationNum"];td2.title = json_data[i]["stationNum"];td2.style.width = "8%";td2.style.textAlign = "center";
        td3.innerHTML = json_data[i]["stationName"];td3.title = json_data[i]["stationName"];td3.style.width = "8%";td3.style.textAlign = "center";
        td4.innerHTML = json_data[i]["category"];td4.title = json_data[i]["category"];td4.style.width="8%";td4.style.textAlign = "center";
        td5.innerHTML = json_data[i]["position_statement"];td5.title = json_data[i]["position_statement"];td5.style.width = "30%";
        td6.innerHTML = json_data[i]["job_description"];td6.title = json_data[i]["job_description"];td6.style.width="30%";
        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
        t_table.appendChild(tr);
    }

}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

// 全选与反全选
function check_all_not() {
    var check_box_list = document.getElementsByClassName("check_box");
    if (document.getElementById("check_all").checked) {
        for (var t=0; t<check_box_list.length; t++) {
            check_box_list[t].checked = true;
        }
    }else {
        for (var j=0; j<check_box_list.length; j++) {
            check_box_list[j].checked = false;
        }
    }
}

// 批量删除数据
function delete_data() {
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0){
        document.getElementById("close-model2").click();
        return
    }
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/set_up.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("delete_list="+String(myArray)+"&page=1");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], page);
                document.getElementById("check_all").checked = false;
            }
        }
    };
}

// 确认删除
function Confirm_delete() {
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0){
        document.getElementById("Confirm-content").innerHTML = "<i style='color: #1E90FF;font-size: 50px;vertical-align:middle;' class=\"iconfont icon-jinggao-chucuo\"></i> <span>请选择需要删除的岗位！</span>";
    }else {
        document.getElementById("Confirm-content").innerHTML = "<i style='color: #1E90FF;font-size: 50px;vertical-align:middle;' class=\"iconfont icon-jinggao-chucuo\"></i> <span>确认要删除这"+String(myArray.length)+"个岗位吗？</span>"
    }
}
